home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6038 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.5 KB  |  134 lines

  1. Path: news.mindlink.net!news
  2. From: genew@mindlink.bc.ca (Gene Wirchenko)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: More Modulus questions
  5. Date: Thu, 22 Feb 1996 06:59:51 GMT
  6. Organization: MIND LINK! - British Columbia, Canada
  7. Message-ID: <4gh4eh$gsj@fountain.mindlink.net>
  8. References: <Pine.SOL.3.90.960219171637.21117B-100000@eddie> <4gfnka$ni7@spanky.pls.ov.com> <4gfp5a$r8e@cloner2.ix.netcom.com>
  9. NNTP-Posting-Host: line172.nwm.mindlink.net
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. wzjn@ix.netcom.com (KPN ) wrote:
  13.  
  14. [snip]
  15.  
  16. >Here again is my problem: (From æHow to Program CÆ second edition
  17. >Deitel/Dietel - Write a program that reads an integer and determines,
  18. >and prints how many digits in the integer are 7Æs)
  19. >#include <stdio.h>
  20.  
  21. >main()
  22.  
  23.      int main(void)
  24. is clearer.
  25.  
  26. >{
  27. >  int number;
  28. >  int first, second, third, fourth;
  29. >  int integer1, integer2, integer3, integer4; 
  30.  
  31. >  printf("Enter a four digit number: ");
  32. >  scanf("%d", &number);
  33.  
  34. >/*
  35. >   Split the four digit number into four seperate integers
  36. >*/
  37.  
  38. >  first = number / 1000;
  39. >  integer1 = number % 1000;
  40.  
  41. >  second = integer1 / 100;
  42. >  integer2 = integer1 % 100;
  43.  
  44. >  third = integer2 / 10;
  45. >  integer3 = integer2 % 10;
  46.  
  47. >  fourth = integer3 / 1;
  48. >  integer4 = integer3 % 1;
  49.  
  50.      Replace previous two lines of code with:
  51.           fourth=integer3;
  52. Dividing by 1 is boring.
  53.  
  54. >/*
  55. >   Use modulas to determine if any integers are a 7
  56. >*/
  57.  
  58.      This doesn't work.  7%7 is the remainder from 7 divided by 7: 0.
  59. 0 is false and the if goes to the else.  The logic is precisely
  60. reverse if the digit is not 7.
  61.  
  62. >      
  63. >   if (first % 7)
  64.  
  65.      Try:
  66.           if (first==7)
  67. and same with the others.
  68.  
  69. >      printf("First integer was a 7\n");
  70. >   else
  71. >      printf("Not a 7\n");
  72.  
  73. >if (second % 7)
  74. >      printf("second integer was a 7\n");
  75. >   else
  76. >      printf("Not a 7\n");
  77.  
  78. >if (third % 7)
  79. >      printf("third integer was a 7\n");
  80. >   else
  81. >      printf("Not a 7\n");
  82.  
  83. >if (fourth % 7)
  84. >      printf("fourth integer was a 7\n");
  85. >   else
  86. >      printf("Not a 7\n");
  87.  
  88.  
  89. >Yes, I know that you may have a different style/ would write it
  90. >differently / wouldnÆt do it this way, but thatÆs OK - IÆll learn. All
  91. >that IÆm concerned about right now, is that if I enter the number 7000,
  92. >it tells me that the first digit is not a seven, and all the rest are a
  93. >7.
  94.  
  95.      Please note that program was supposed to COUNT the number of 7s.
  96. So you really ought to do something like:
  97.  
  98.           int howmany;
  99.           ...
  100.           howmany=0
  101.           if (first==7)
  102.                howmany++;
  103.           if (second==7)
  104.                howmany++;
  105. and so on.
  106.           printf("%d has %d sevens.\n", number,howmany);
  107.  
  108. >Could this be explained to me in verbiage instead of a big complicated
  109. >example using all sorts of code that I do not know yet? I know, that in
  110. >the book IÆm teaching myself from, more than a few examples have IF
  111. >statement conditioned by a Modulus code ... how (in words!!!) does this
  112. >code work?
  113.  
  114.      I have not written the code the way I would do it, but have just
  115. corrected your worst.  Have another look at it because...
  116.      Your approach is deficient in that it will not handle bigger
  117. numbers.  If you have not yet covered looping, forget this until then
  118. and then come back to it.  If you have, you have the tools to deal
  119. with it.  If you need further assistance, post code here or in E-mail.
  120.  
  121. >Thank you for your patience and expertise,
  122.  
  123. >kevin
  124.  
  125. Sincerely,
  126.  
  127. Gene Wirchenko
  128.  
  129. C Pronunciation Guide:
  130.      y=x++;     "wye equals ex plus plus semicolon"
  131.      x=x++;     "ex equals ex doublecross semicolon"
  132.  
  133.